package cn.iocoder.yudao.module.famous.doctor.controller.app.treatmentuser;

import cn.hutool.core.util.IdcardUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroupuser.vo.ResearchgroupUserByClinic;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.treatmentuser.vo.*;
import cn.iocoder.yudao.module.famous.doctor.convert.treatmentuser.TreatmentUserConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.treatmentuser.TreatmentUserDO;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.researchgroupuser.ResearchgroupUserMapper;
import cn.iocoder.yudao.module.famous.doctor.service.treatmentuser.TreatmentUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.setLoginclinicUser;

@Tag(name = "用户 APP - 就诊人")
@RestController
@RequestMapping("/famous.doctor/treatment-user/app")
@Validated
public class AppTreatmentUserController {

    @Resource
    private TreatmentUserService treatmentUserService;

    @Resource
    private ResearchgroupUserMapper researchgroupUserMapper;

    @PostMapping("/create")
    @Operation(summary = "创建就诊人")
    public CommonResult<Long> createTreatmentUser(@Valid @RequestBody TreatmentUserCreateReqVO createReqVO) {
        return success(treatmentUserService.createTreatmentUser(createReqVO));
    }
    @PostMapping("/notLoginCreate")
    @Operation(summary = "创建就诊人")
    public CommonResult<Long> notLoginCreateTreatmentUser(@Valid @RequestBody TreatmentUserCreateReqVO createReqVO) {
        return success(treatmentUserService.createNotLoginTreatmentUser(createReqVO));
    }
    @GetMapping("/verifyIdentityCard")
    @Operation(summary = "身份证检验")
    public CommonResult<IdcardUtil.Idcard> verifyIdentityCard(@Valid @RequestParam("card") String card) {
        return success(treatmentUserService.verifyIdentityCard(card));
    }

    @PutMapping("/update")
    @Operation(summary = "更新就诊人")
    public CommonResult<Boolean> updateTreatmentUser(@Valid @RequestBody TreatmentUserUpdateReqVO updateReqVO) {
        treatmentUserService.updateTreatmentUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除就诊人")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTreatmentUser(@RequestParam("id") Long id) {
        treatmentUserService.deleteTreatmentUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得就诊人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TreatmentUserRespVO> getTreatmentUser(@Valid TreatmentUserReqVO treatmentUserReqVO) {
//        TreatmentUserDO treatmentUser = treatmentUserService.getTreatmentUser(id);
        TreatmentUserDO treatmentUser= treatmentUserService.getTreatmentUserVo(treatmentUserReqVO);
        return success(TreatmentUserConvert.INSTANCE.convert(treatmentUser));
    }

    @GetMapping("/getByidentityCard")
    @Operation(summary = "根据身份证获取就诊人")
    @Parameter(name = "identityCard", description = "编号", required = true, example = "1024")
    public CommonResult<TreatmentUserRespVO> getTreatmentUserByidentityCard(@RequestParam("identityCard") String identityCard) {
        TreatmentUserDO treatmentUser = treatmentUserService.getTreatmentUserByidentityCard(identityCard);
        return success(TreatmentUserConvert.INSTANCE.convert(treatmentUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得就诊人列表")
    public CommonResult<List<TreatmentUserRespVO>> getTreatmentUserList() {
        List<TreatmentUserDO> list = treatmentUserService.getTreatmentUserList();
        List<TreatmentUserRespVO> treatmentUserRespVOS = TreatmentUserConvert.INSTANCE.convertList(list);
        treatmentUserRespVOS.forEach(treatmentUserRespVO -> {
            List<ResearchgroupUserByClinic> researchgroupUserByClinics = researchgroupUserMapper.selectClinicList(4L, treatmentUserRespVO.getIdentityCard());
            if (researchgroupUserByClinics.size()>0) {
                treatmentUserRespVO.setIsClinic(true);
            }else {
                treatmentUserRespVO.setIsClinic(false);
            }
        });
        return success(treatmentUserRespVOS);
    }

    @GetMapping("/setClinic")
    @Operation(summary = "选择当前就诊人")
    @Parameter(name = "id", description = "就诊人id", required = true, example = "1024")
    public CommonResult<Boolean> setClinic(@RequestParam("id") Long id) {
        // setLoginclinicUser(getLoginUser(),id,request);
        treatmentUserService.setClinic(id);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得就诊人分页")
    public CommonResult<PageResult<TreatmentUserRespVO>> getTreatmentUserPage(@Valid TreatmentUserPageReqVO pageVO) {
        PageResult<TreatmentUserDO> pageResult = treatmentUserService.getTreatmentUserPage(pageVO);
        PageResult<TreatmentUserRespVO> treatmentUserRespVOPageResult = TreatmentUserConvert.INSTANCE.convertPage(pageResult);
        treatmentUserRespVOPageResult.getList().forEach(treatmentUserRespVO -> {
            List<ResearchgroupUserByClinic> researchgroupUserByClinics = researchgroupUserMapper.selectClinicList(4L, treatmentUserRespVO.getIdentityCard());
            if (researchgroupUserByClinics.size()>0) {
                treatmentUserRespVO.setIsClinic(true);
            }else {
                treatmentUserRespVO.setIsClinic(false);
            }
        });
        return success(treatmentUserRespVOPageResult);
    }

}
